﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using System.Globalization;
using NavigationSampleApp.Properties;
using Smart.Windows.Navigation;

namespace NavigationSampleApp
{
    public partial class MainForm : Form, IFunctionControl
    {
        private readonly Navigator navigator;

        private readonly Dictionary<Keys, FunctionKey> enabledFunctions = new Dictionary<Keys, FunctionKey>();
        private readonly List<Button> functionButtons = new List<Button>();        

        /// <summary>
        /// 
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            InitializeFunctionKeys();

            // ナビゲーター初期化
            this.navigator = new Navigator( new ControlViewProvider( ViewPanel ) );
            this.navigator.Forwarding += OnForwarding;
            this.navigator.Exited += OnExited;

            this.navigator.AutoRegister( Assembly.GetExecutingAssembly() );

            // 最初の画面へ遷移
            Show();
            this.navigator.Forward( Views.Menu );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnForwarding(object sender, ViewForwardEventArgs e)
        {
            // アプリケーション固有設定
            TitleLabel.Text = Resources.ResourceManager.GetString( "View_" + e.ViewId.ToString(), CultureInfo.CurrentCulture );

            IApplicationView view = this.navigator.CurrentView as IApplicationView;
            if( view != null )
            {
                view.SetFunctionControl( this );
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnExited(object sender, ViewExitEventArgs e)
        {
            Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnButtonExit(object sender, EventArgs e)
        {
            IApplicationView view = this.navigator.CurrentView as IApplicationView;
            if( view != null )
            {
                view.OnButtonExit();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void InitializeFunctionKeys()
        {
            Fn1Button.Tag = Keys.F1;
            Fn2Button.Tag = Keys.F2;
            Fn3Button.Tag = Keys.F3;
            Fn4Button.Tag = Keys.F4;
            Fn5Button.Tag = Keys.F5;
            Fn6Button.Tag = Keys.F6;
            Fn7Button.Tag = Keys.F7;
            Fn8Button.Tag = Keys.F8;
            Fn9Button.Tag = Keys.F9;
            Fn10Button.Tag = Keys.F10;
            Fn11Button.Tag = Keys.F11;
            Fn12Button.Tag = Keys.F12;
            functionButtons.Add( Fn1Button );
            functionButtons.Add( Fn2Button );
            functionButtons.Add( Fn3Button );
            functionButtons.Add( Fn4Button );
            functionButtons.Add( Fn5Button );
            functionButtons.Add( Fn6Button );
            functionButtons.Add( Fn7Button );
            functionButtons.Add( Fn8Button );
            functionButtons.Add( Fn9Button );
            functionButtons.Add( Fn10Button );
            functionButtons.Add( Fn11Button );
            functionButtons.Add( Fn12Button );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keys"></param>
        public void UpdateFunctionKeys(FunctionKey[] keys)
        {
            enabledFunctions.Clear();
            if( keys != null )
            {
                foreach( FunctionKey key in keys )
                {
                    enabledFunctions.Add( key.Keys, key );                    
                }                
            }        
    
            foreach( Button button in functionButtons )
            {
                Keys keyData = (Keys)button.Tag;
                if( enabledFunctions.ContainsKey( keyData ) )
                {
                    FunctionKey key = enabledFunctions[ keyData ];
                    button.Enabled = true;
                    button.Text = key.Display   ;
                }
                else
                {
                    button.Enabled = false;
                    button.Text = "";
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnFunctionClick(object sender, EventArgs e)
        {
            Button button = sender as Button;

            // ファンクションディスパッチ
            IApplicationView view = this.navigator.CurrentView as IApplicationView;
            if( ( view != null ) && ( button != null ) )
            {
                view.OnFunctionKey( (Keys)button.Tag );
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if( enabledFunctions.ContainsKey( keyData ) )
            {
                // ファンクションディスパッチ
                IApplicationView view = this.navigator.CurrentView as IApplicationView;
                if( view != null )
                {
                    view.OnFunctionKey( keyData );
                    return true;
                }
            }

            return base.ProcessDialogKey( keyData );
        }
    }
}